package org.bakerstreet.db.connection.tests;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.bakerstreet.db.connection.Connector;

import junit.framework.TestCase;


public class ConnectionTests extends TestCase{
	
	
	public static final String[] USERS = new String[]{"id","name","login","pass","type"};  
	
	
	public void testOpen(){
		Connector conn = new Connector();
		assertEquals(true , conn.open());
	} 
	
	public void testClose(){
		Connector conn = new Connector();
		conn.open();
		assertEquals(true , conn.close());
	}
	
	public void testiscard(){
		Connector conn = new Connector();
		
		conn.open();
		
		Map<String,String> campos = new HashMap<String,String>();
		
		//campos.put("id", "1");
		campos.put("name", "Popolus Panatinaikos");
		campos.put("login", "popolus");
		campos.put("pass", "1");
		campos.put("type", "0");
		
		assertEquals(true , conn.discard());
		
		conn.close();
		
	}
	
	private void selectTosco(){
		Connector conn = new Connector();
		
		conn.open();

		ResultSet rs = conn.freeSelect("SELECT * FROM users;");
		int ind = 0;
		try {
			 while(rs.next()){
				 ind++;
				 System.out.println(rs.getString("name")+"   "+ind);
			 }
			
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public void testInsert() {
		
		Connector conn = new Connector();
		
		conn.open();
		
		Map<String,String> campos = new HashMap<String,String>();
		
		//campos.put("id", "1");
		campos.put("name", "Popolus Panatinaikos");
		campos.put("login", "popolus");
		campos.put("pass", "1");
		campos.put("type", "0");
		
		assertEquals(true, conn.insert("users", campos));
		
		conn.discard();
		conn.close();
	}
	
	public void testDelete(){
		Connector conn = new Connector();
		
		conn.open();
		
		Map<String,String> campos = new HashMap<String,String>();
		
		//campos.put("id", "1");
		campos.put("name", "Popolus Panatinaikos");
		campos.put("login", "popolus");
		campos.put("pass", "1");
		campos.put("type", "0");
		
		conn.insert("users", campos);
		
		
		assertEquals(true,conn.delete("users", campos));
		
		conn.discard();
		conn.close();
		
	}
	
	public void testFilterData(){
		
		
		Connector conn = new Connector();
		conn.open();
		

		Map<String,String> user1 = new HashMap<String,String>();
		user1.put("name", "Popolus Panatinaikos");
		user1.put("login", "popolus");
		user1.put("pass", "1");
		user1.put("type", "0");
		
		conn.insert("users", user1);
		
		Map<String,String> user2 = new HashMap<String,String>();
		user2.put("name", "Geronimo Von Perov");
		user2.put("login", "Von");
		user2.put("pass", "perov");
		user2.put("type", "0");
		
		conn.insert("users", user2);
		
		List<Map<String,String>> data  = conn.filterData("users", USERS,user2);
		
		for (Map<String, String> record : data) {
			for (int i = 0; i < USERS.length; i++) {
				assertNotNull(record.get(USERS[i]));
				System.out.println(record.get(USERS[i]));
			}
			
			System.out.println("****************************");
		}
		
		
		conn.discard();
		conn.close();
	}
	
	
	
	public void testUpdate(){
		Connector conn = new Connector();
		conn.open();
		

		Map<String,String> user1 = new HashMap<String,String>();
		user1.put("name", "Popolus Panatinaikos");
		user1.put("login", "popolus");
		user1.put("pass", "1");
		user1.put("type", "0");
		
		conn.insert("users", user1);
		
		Map<String,String> toUpdate = new HashMap<String,String>();
		toUpdate.put("name", "Galeto Empanado");
		
		
		conn.update("users",toUpdate, user1);
		
		List<Map<String,String>> data  = conn.filterData("users", USERS,user1);
		
		for (Map<String, String> record : data) {
			assertEquals("Galeto Empanado", record.get("name"));
		}
		
		conn.discard();
		conn.close();
		
	}
	
}
